package com.engine.cese.duss.srm.offline.cmd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Param:
 * @return:
 * @Author: Dusongsong
 * @Date: 2021/3/1
 */

public class LeftTreeCmd extends AbstractCommonCommand<Map<String, Object>> {

    public LeftTreeCmd(User user, Map<String, Object> params) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        if (null == user) {
            apidatas.put( "hasRight", false );
            return apidatas;
        }
        apidatas.put( "hasRight", true );
        try {
            String selectedKey = Util.null2String( params.get( "selectedKey" ) );
            String userid = Util.null2String( user.getUID());
            JSONArray datasArray = getLeftTreeData( selectedKey,userid );
            apidatas.put( "flag", true );
            apidatas.put( "datas", datasArray.toJSONString() );
        } catch (Exception e) {
            apidatas.put( "flag", false );
            e.printStackTrace();
        }
        return apidatas;
    }

    public static JSONArray getLeftTreeData(String selectedKey,String userid) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String xxcglx = baseBean.getPropValue("SrmWfTab","xxcglx");
        JSONArray datasArray = new JSONArray();
        String sql ="select substr(lxrq,0,4) as nd from "+xxcglx+" where 1=1";
        String sqlwhere = " and xmzt='"+selectedKey+"' and lxsqr="+userid+"";
        String sqlgroup = " group by substr(lxrq,0,4)";
        sql = "3".equals(selectedKey)?sql+sqlgroup:sql+sqlwhere+sqlgroup;
        rs.executeQuery(sql);
        while (rs.next()){
            String nd = Util.null2String(rs.getString("nd"));//年度
            JSONObject datasJson = new JSONObject();
            JSONArray childsArray = new JSONArray();
            datasJson.put( "domid", selectedKey+"_"+ nd );
            datasJson.put( "haschild", true );
            datasJson.put( "isopen", true );
            datasJson.put( "key",  "type_"+selectedKey+"_"+ nd);
            datasJson.put( "name", nd );
            String sql1="select requestid,cglxmc from "+xxcglx+" where 1=1";
            String sql1where = " and xmzt='"+selectedKey+"' and substr(lxrq,0,4)='"+nd+"' and lxsqr='"+userid+"'";
            String order = " order by cglxmc";
            sql1 = "3".equals(selectedKey)?sql1+order:sql1+sql1where+order;
            rs1.executeQuery(sql1);
            while (rs1.next()){
                String requestid = Util.null2String(rs1.getString("requestid"));//requestid
                String cglxmc = Util.null2String(rs1.getString("cglxmc"));//项目名称
                JSONObject childsJson = new JSONObject();
                String domid = requestid;
                boolean haschild = false;
                boolean isopen = false;
                String key = "xm_"+nd+"_"+ requestid;
                String name = cglxmc;
                childsJson.put( "domid", domid );
                childsJson.put( "haschild", haschild );
                childsJson.put( "isopen", isopen );
                childsJson.put( "key", key );
                childsJson.put( "name", name );
                childsArray.add( childsJson );
            }
            datasJson.put( "childs", childsArray );
            datasArray.add( datasJson );
        }
        return datasArray;
    }
}
